<?php
namespace app\dc\controller;

use think\Controller;
use think\Db;

class Order  extends Base
{


    public function index()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('order');
            $dc = Db::name('dc');
            $biz = Db::name('biz');

            $res = $gd->page($page, $limit)
                ->order('id', 'desc')
              ->select();
            foreach ($res as $index => $re) {
                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
                $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
                $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
                $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
            }
//            $res4 = $gd->page($page, $limit)
//                ->order('id', 'desc')
//                ->select();
//            $res0 = $gd->page($page, $limit)
//                ->where('status',0)
//                ->order('id', 'desc')
//                ->select();
//            $res1 = $gd->page($page, $limit)
//                ->where('status',1)
//                ->order('id', 'desc')
//                ->select();
//            $res2 = $gd->page($page, $limit)
//                ->where('status',2)
//                ->order('id', 'desc')
//                ->select();
//            $res3 = $gd->page($page, $limit)
//                ->where('status',3)
//                ->order('id', 'desc')
//                ->select();
//            foreach ($res4 as $index => $re) {
//                $res4[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
//                $res4[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
//                $res4[$index]['name1']=$dc->where('id', $res4[$index]['dc_id'])->value('name');
//                $res4[$index]['name2']=$biz->where('id', $res4[$index]['biz_id'])->value('name');
//            }
//            foreach ($res0 as $index => $re) {
//                $res0[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
//                $res0[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
//                $res0[$index]['name1']=$dc->where('id', $res0[$index]['dc_id'])->value('name');
//                $res0[$index]['name2']=$biz->where('id', $res0[$index]['biz_id'])->value('name');
//            }
//            foreach ($res1 as $index => $re) {
//                $res1[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
//                $res1[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
//                $res1[$index]['name1']=$dc->where('id', $res1[$index]['dc_id'])->value('name');
//                $res1[$index]['name2']=$biz->where('id', $res1[$index]['biz_id'])->value('name');
//            }
//            foreach ($res2 as $index => $re) {
//                $res2[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
//                $res2[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
//                $res2[$index]['name1']=$dc->where('id', $res2[$index]['dc_id'])->value('name');
//                $res2[$index]['name2']=$biz->where('id', $res2[$index]['biz_id'])->value('name');
//            }
//            foreach ($res3 as $index => $re) {
//                $res3[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
//                $res3[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
//                $res3[$index]['name1']=$dc->where('id', $res3[$index]['dc_id'])->value('name');
//                $res3[$index]['name2']=$biz->where('id', $res3[$index]['biz_id'])->value('name');
//            }
//
//
//            $res=['res4'=>$res4,'res0'=>$res0,'res1'=>$res1,'res2'=>$res2,'res3'=>$res3];
//
//            return json($res);
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('order')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    public function index1()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('order');
            $dc = Db::name('dc');
            $biz = Db::name('biz');

            $res = $gd->page($page, $limit)
                ->where('status',0)
                ->order('id', 'desc')
                ->select();
            foreach ($res as $index => $re) {
                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
                $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
                $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
                $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
            }

            $this->success('ok', '', $res);

        } else {
            $count = Db::name('order') ->where('status',1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    public function index2()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('order');
            $dc = Db::name('dc');
            $biz = Db::name('biz');

            $res = $gd->page($page, $limit)
                ->where('status',1)
                ->order('id', 'desc')
                ->select();
            foreach ($res as $index => $re) {
                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
                $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
                $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
                $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
            }

            $this->success('ok', '', $res);

        } else {
            $count = Db::name('order') ->where('status',2)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    public function index3()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('order');
            $dc = Db::name('dc');
            $biz = Db::name('biz');

            $res = $gd->page($page, $limit)
                ->where('status',2)
                ->order('id', 'desc')
                ->select();
            foreach ($res as $index => $re) {
                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
                $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
                $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
                $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
            }

            $this->success('ok', '', $res);

        } else {
            $count = Db::name('order') ->where('status',3)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    public function index4()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('order');
            $dc = Db::name('dc');
            $biz = Db::name('biz');

            $res = $gd->page($page, $limit)
                ->where('status',3)
                ->order('id', 'desc')
                ->select();
            foreach ($res as $index => $re) {
                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
                $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
                $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
                $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
            }

            $this->success('ok', '', $res);

        } else {
            $count = Db::name('order') ->where('status',4)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }




    //处理中 已完成  取消  按钮编辑
    public function handle()
    {
        if (request()->isAjax()) {

            $data = input('data_id');
            //   $id=$data['id'];
            $res = Db::name('order')

                ->where('id',$data)->update(["status"=>1]);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');
        }
    }
    public function finish()
    {
        if (request()->isAjax()) {

            $data = input('data_id');
            //   $id=$data['id'];
            $res = Db::name('order')

                ->where('id',$data)->update(["status"=>2]);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');
        }
    }
    public function cannel()
    {
        if (request()->isAjax()) {

            $data = input('data_id');
            //   $id=$data['id'];
            $res = Db::name('order')

                ->where('id',$data)->update(["status"=>3]);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');
        }
    }
    //搜索
    public function base_so()
    {
        $status[0] = '禁用';
        $status[1] = '正常';
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }

        $res = Db::name('order')->page($page, $limit)
            //搜索相关
            //参数绑定
            // ->where('id > :id AND name LIKE :name ', ['id' => 0, 'name' => 'thinkphp%'])
            ->where('order_id LIKE :key1 OR id LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
            ->select();
        $dc = Db::name('dc');
        $biz = Db::name('biz');
        foreach ($res as $index => $re) {
            $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
            $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
            $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
            $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
        }
        //根据关键字返回 相应数据
        $total =
            Db::name('order')
                ->where('order_id LIKE :key1 OR id LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
                ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
    }

    //详情
    public function detail1()
    {
        $id = input('id');
        $order = Db::name('order')
            ->where('id', $id)->find();
        $this->assign('order', $order);

        $biz = Db::name('biz')->where('id', $order['biz_id'])->field('name,linkman,mobile,address')->find();

        $this->assign('biz', $biz);

        $list = Db::name('goodslist')->where('order_id', $order['order_id'])->select();

        $this->assign('list', $list);


    }





    public function detail()
    {

        if (request()->isAjax()) {
            $data = input('');
            unset($data['name']);
            $res = Db::name('supply')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $order = Db::name('order')->find($id);
            $biz=Db::name('biz')->where('id',$order['biz_id'])->field('name,linkman,mobile,address')->find();

            $this->assign('biz', $biz);

            $list = Db::name('goodslist')->where('order_id', $order['order_id'])->select();

            $this->assign('list', $list);

            $this->assign('order', $order);
            return $this->fetch();
        }


    }


}
